package com.hod.reservgrid.client.model;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.CellMetadata;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.EditorGridPanel;
import com.gwtext.client.widgets.grid.GridEditor;
import com.gwtext.client.widgets.grid.Renderer;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtextux.client.data.PagingMemoryProxy;


public class BedGrid extends EditorGridPanel {		
	protected static GearsDataStore dsBedType = (GearsDataStore) GWT.create(BedType.class);
	private String idRoom;
	private ColumnModel columnModel = getColumnConfigs();
	protected Store store;
	private GearsDataStore ds = null;

	protected ColumnModel getColumnConfigs() {
        ColumnModel columnModel = new ColumnModel(columns);
        return columnModel;
    }	
	private static Renderer rndrBedType = new Renderer() {
        public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
        	BedType o;
			try {
				o = (BedType)dsBedType .findById(new Integer(record.getAsString("idBedType")));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				return "";
			} catch (DatabaseException e) {
				// TODO Auto-generated catch block
				return "";
			} 
            return o.getDescription();
        }
    };

	
    
    private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{
                    new IntegerFieldDef("idBed"),
                    new IntegerFieldDef("number"),
                    new IntegerFieldDef("idRoom"),
                    new IntegerFieldDef("idBedType"),                    
                    new StringFieldDef("state"),
            }
    );
    
    protected static BaseColumnConfig[] columns = new BaseColumnConfig[]{
            new ColumnConfig("idBed", "idBed", 60, true, null, "idBed"),
            new ColumnConfig("number", "number", 60, true, null, "number"),
            new ColumnConfig("idBedType", "idBedType", 60, true, null, "idBedType"),
            new ColumnConfig("state", "state", 60, true, null, "state"),
    };

	

	public BedGrid() {      
    }

    public BedGrid(boolean small,GearsDataStore ds,String idRoom) {
    	this.ds = ds;     	
    	getData(idRoom);

        columnModel = getColumnConfigs();
        setColumnModel(columnModel);
        final RowSelectionModel sm = new RowSelectionModel(true);
        setSelectionModel(sm);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");
        createToolbar();

        
    }
    
    protected void createToolbar(){
    	Toolbar toolbar = new Toolbar();

        ToolbarButton button = new ToolbarButton("Add Bed", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	   try {
						Window bed = new Window();
						bed.setLayout(new AnchorLayout());
						bed.setWidth("40%");
						bed.setHeight("30%");
						BedCRUD resCRUD = new BedCRUD();
						bed.add(resCRUD.createFormPanel());
						resCRUD.setIdRoom(Integer.valueOf(idRoom));
						bed.show();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					com.google.gwt.user.client.Window.alert(e1.getMessage());
				}
            	
            	
            }
        });
        
        toolbar.addButton(button);
        setTopToolbar(toolbar);
    }
    
    protected void getData(){
    	getData("");
    }
    
    protected void getData(String idRoom){
    	PagingMemoryProxy proxy = null;
		try {
			this.idRoom = idRoom;
			GearsQuery q = null;
			if (idRoom.length()>0){				
				q = this.ds.getNamedQuery(Bed.FIND_BY_ROOM);
				q.setString(0, idRoom);				
			}else{
				q = this.ds.getNamedQuery(Bed.FIND_ALL);
			}
			Bed[] data = (Bed[]) q.execute();
			Bed rs = null;	
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	               
                data2[i] = new Object[]{rs.getIdBed(),rs.getNumber(),rs.getIdRoom(),rs.getIdBedType(),rs.getState()};
            }
			proxy = new PagingMemoryProxy(data2);
			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			com.google.gwt.user.client.Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);        
        store = new Store(proxy, reader);
        store.load();
        setStore(store);        
    }
    
    
    public void refresh(String id){
	    //this.clear();
    	getData(id);	
    	reconfigure(store, columnModel);
        this.getView().refresh(true);
    }
    
}
